package com.ys.service.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.bo.dto.NameValueSelectDto;
import com.ys.dto.DtreeSqlDto;
import com.ys.entity.business.TbBaseInfo;
import com.ys.entity.sys.YsCol;
import com.ys.entity.sys.YsDict;
import com.ys.entity.sys.YsRowClickGridCol;
import com.ys.entity.sys.YsMenu;
import com.ys.entity.sys.YsModalSelector;
import com.ys.enume.base.TbFileInfoEnum;
import com.ys.enume.sys.YsColEnum;
import com.ys.enume.sys.YsRowClickGridColEnum;
import com.ys.enume.sys.YsMenuEnum;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.mapper.sys.YsDictMapper;
import com.ys.mapper.sys.YsRowClickGridColMapper;
import com.ys.service.sys.YsRowClickGridColService;
import com.ys.vo.R;
import com.ys.vo.W;

@Service("ysRowClickGridColService")
public class YsRowClickGridColServiceImpl implements YsRowClickGridColService {

	@Autowired
	private DiySqlMapper diySqlMapper;
	@Autowired
	private YsDictMapper ysDictMapper;
	@Autowired
	private YsRowClickGridColMapper ysRowClickGridColMapper;

	@Override
	public DtreeSqlDto dtree() {
		
		DtreeSqlDto.StatusDto status = DtreeSqlDto.StatusDto.init(200, "");

		List<DtreeSqlDto.DataDto> data = new ArrayList<DtreeSqlDto.DataDto>();
		List<YsMenu> listYsMenu = new YsMenu()
				.setOrderby("A.Lev asc, A.SortNo asc")
				.queryList();
		
		// 一级菜单
		for(YsMenu ysMenu1 : listYsMenu) {
			if (ysMenu1.getLev() != YsMenuEnum.Lev.ONE.value) {
				continue;
			}
			String id1 = ysMenu1.getId().toString();
			String title1 = ysMenu1.getName();
			String parentId1 = ysMenu1.getPid().toString();
			
			// 二级菜单
			List<DtreeSqlDto.DataDto> children = new ArrayList<DtreeSqlDto.DataDto>();
			for (YsMenu ysMenu2 : listYsMenu) {
				if (ysMenu2.getPid().longValue() != ysMenu1.getId().longValue()) {
					continue;
				}
				String id2 = ysMenu2.getId().toString();
				String title2 = ysMenu2.getName();
				String parentId2 = ysMenu2.getPid().toString();
				
				// 三级菜单
				List<DtreeSqlDto.DataDto> children1 = new ArrayList<DtreeSqlDto.DataDto>();
				for (YsMenu ysMenu3 : listYsMenu) {
					if (ysMenu3.getPid().longValue() != ysMenu2.getId().longValue()) {
						continue;
					}
					String id3 = ysMenu3.getId().toString();
					String title3 = ysMenu3.getName();
					String parentId3 = ysMenu3.getPid().toString();
					List<DtreeSqlDto.DataDto> children2 = null;
					DtreeSqlDto.DataDto dto = new DtreeSqlDto.DataDto(id3, title3, parentId3, DtreeSqlDto.DataDto.initCheckArr(null), children2);
					children1.add(dto);
				}
				
				DtreeSqlDto.DataDto dataDto2 = new DtreeSqlDto.DataDto(id2, title2, parentId2, DtreeSqlDto.DataDto.initCheckArr(null), children1);
				children.add(dataDto2);
			}
			
			DtreeSqlDto.DataDto dataDto1 = new DtreeSqlDto.DataDto(id1, title1, parentId1, DtreeSqlDto.DataDto.initCheckArr(null), children);
			data.add(dataDto1);
		}
		return new DtreeSqlDto(status, data);
	}
	
	@Override
	public R add(Long rowClickGridId,
			String name,
			String code,
			Integer sortNo,
			String frontType,
			Integer frontEdit,
			Integer frontWidth,
			Integer frontAlign,
			Integer frontShow,
			String dictKeyBase,
			String modalKey,
			String modalRelativeCode,
			String modalValueCode,
			Integer multyFileSourceType) {
		
		String tplScript = null;
		YsRowClickGridCol entity = YsRowClickGridCol.ysRowClickGridCol(rowClickGridId, name, code, sortNo, frontType, frontEdit, frontWidth, frontAlign, frontShow,
				
				dictKeyBase, modalKey, modalRelativeCode, modalValueCode, multyFileSourceType, tplScript,1);

		ysRowClickGridColMapper.insert(entity);
		return R.ok("操作成功");
	}

	@Override
	public R deleteTrue(Long id) {
		ysRowClickGridColMapper.deleteTrue(id);
		return R.ok();
	}

	@Override
	public String toSettingOption(Model model, Long id) {
		
		if (id == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsRowClickGridCol entity = new YsRowClickGridCol().setId(id).queryFirst();
		if (!YsRowClickGridColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(entity.getFrontType())
				&& !YsRowClickGridColEnum.FrontType.SELECTOR_SWITCH.value.equals(entity.getFrontType())) {
			return W.error(model, "非下拉框类型无法设置");
		}
		
		// 字典数组
		List<ToSettingOptionDto.RowDto> rows = new ArrayList<ToSettingOptionDto.RowDto>();
		String dictKeyBase = entity.getDictKeyBase();
		if (dictKeyBase != null && dictKeyBase.length() > 0) {
			
			List<YsDict> listYsDict = new YsDict().setDictKey(dictKeyBase)
					.setOrderby("A.SortNo asc")
					.queryList();
			
			for(YsDict ysDict : listYsDict) {
				Long id2 = ysDict.getId();
				String dictName = ysDict.getDictName();
				String dictValue = ysDict.getDictValue();
				rows.add(new ToSettingOptionDto.RowDto(id2, dictName, dictValue));
			}
		}
		
		String name = entity.getName();
		String code = entity.getCode();
		ToSettingOptionDto dto = new ToSettingOptionDto(id, name, code, dictKeyBase, rows);
		model.addAttribute("dto", dto);
		return "sys/ysRowClickGridCol/toSettingOption";
	}

	@Override
	public String toSettingForm(Model model, Long menuId) {
		
		if (menuId == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsMenu entity = new YsMenu().setId(menuId).queryFirst();
		if (entity.getLev() != YsMenuEnum.Lev.THREE.value) {
			return W.error(model, "非三级菜单无法设置");
		}
		
		// 新增表单数组
		List<ToSettingFormDto.RowDto> rowsAdd = new ArrayList<ToSettingFormDto.RowDto>();
		List<YsCol> listYsCol = new YsCol().setMenuId(menuId).setFormAddState(YsColEnum.FormAddState.YES.value)
				.setOrderby("A.SortNo asc")
				.queryList();
		for(YsCol ysCol : listYsCol) {
			Long id = ysCol.getId();
			String code = ysCol.getCode();
			String name = ysCol.getName();
			List<NameValueSelectDto> typeArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormAddType e : YsColEnum.FormAddType.values()) {
				boolean isSelected = e.value.equals(ysCol.getFormAddType());
				typeArr.add(new NameValueSelectDto(e.name, e.value, isSelected));
			}
			List<NameValueSelectDto> stateArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormAddState e : YsColEnum.FormAddState.values()) {
				boolean isSelected = e.value == ysCol.getFormAddState();
				stateArr.add(new NameValueSelectDto(e.name, e.value.toString(), isSelected));
			}
			List<NameValueSelectDto> requiredArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormAddRequired e : YsColEnum.FormAddRequired.values()) {
				boolean isSelected = e.value == ysCol.getFormAddRequired();
				requiredArr.add(new NameValueSelectDto(e.name, e.value.toString(), isSelected));
			}
			rowsAdd.add(new ToSettingFormDto.RowDto(id, code, name, typeArr, stateArr, requiredArr));
		}
		
		// 编辑表单数组
		List<ToSettingFormDto.RowDto> rowsEdit = new ArrayList<ToSettingFormDto.RowDto>();
		List<YsCol> listYsCol2 = new YsCol().setMenuId(menuId).setFormEditState(YsColEnum.FormEditState.YES.value)
				.setOrderby("A.SortNo asc")
				.queryList();
		for(YsCol ysCol : listYsCol2) {
			Long id = ysCol.getId();
			String code = ysCol.getCode();
			String name = ysCol.getName();
			List<NameValueSelectDto> typeArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormEditType e : YsColEnum.FormEditType.values()) {
				boolean isSelected = e.value.equals(ysCol.getFormEditType());
				typeArr.add(new NameValueSelectDto(e.name, e.value, isSelected));
			}
			List<NameValueSelectDto> stateArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormEditState e : YsColEnum.FormEditState.values()) {
				boolean isSelected = e.value == ysCol.getFormEditState();
				stateArr.add(new NameValueSelectDto(e.name, e.value.toString(), isSelected));
			}
			List<NameValueSelectDto> requiredArr = new ArrayList<NameValueSelectDto>();
			for(YsColEnum.FormEditRequired e : YsColEnum.FormEditRequired.values()) {
				boolean isSelected = e.value == ysCol.getFormEditRequired();
				requiredArr.add(new NameValueSelectDto(e.name, e.value.toString(), isSelected));
			}
			rowsEdit.add(new ToSettingFormDto.RowDto(id, code, name, typeArr, stateArr, requiredArr));
		}
		
		ToSettingFormDto dto = new ToSettingFormDto(menuId, rowsAdd, rowsEdit);
		model.addAttribute("dto", dto);
		return "sys/ysCol/toSettingOption";
	}

	@Override
	public String toSettingSelectorModal(Model model, Long id) {

		YsRowClickGridCol entity = new YsRowClickGridCol().setId(id).queryFirst();
		
		// 弹出框信息
		ToSettingSelectorModalDto.SelectorModalInfoDto selectorModalInfo = null;
		{
			// 弹出框选项数组
			Long idYsModalSelector = null;
			String modalKey = entity.getModalKey();
			if (modalKey != null && modalKey.length() > 0) {

				YsModalSelector ysModalSelector = new YsModalSelector().setNameTable(modalKey).queryFirst();
				idYsModalSelector = ysModalSelector.getId();
			}
			
			List<NameValueSelectDto> modalArr = new ArrayList<NameValueSelectDto>();
			List<YsModalSelector> listYsModalSelector = new YsModalSelector().queryList();
			for(YsModalSelector ysModalSelector : listYsModalSelector) {
				String name = ysModalSelector.getName() + "(" + ysModalSelector.getNameTable() + ")";
				String value = ysModalSelector.getId().toString();
				boolean isSelected = idYsModalSelector != null ? ysModalSelector.getId().longValue() == idYsModalSelector : false;
				modalArr.add(new NameValueSelectDto(name, value, isSelected));
			}
			
			selectorModalInfo = new ToSettingSelectorModalDto.SelectorModalInfoDto(modalArr);
		}

		String name = entity.getName();
		String code = entity.getCode();
		ToSettingSelectorModalDto dto = new ToSettingSelectorModalDto(id, name, code, selectorModalInfo);
		model.addAttribute("dto", dto);
		
		return "sys/ysRowClickGridCol/toSettingSelectorModal";
	}

	@Override
	public R settingOption(Long id, String dictKeyBase) {
		
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		YsRowClickGridCol entity = new YsRowClickGridCol().setId(id).queryFirst();
		if (entity == null) {
			return R.error("数据不存在");
		} else if (!YsRowClickGridColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(entity.getFrontType())
				&& !YsRowClickGridColEnum.FrontType.SELECTOR_SWITCH.value.equals(entity.getFrontType())) {
			return R.error("非选择框类型的数据");
		}
		
		entity.setDictKeyBase(dictKeyBase);
		ysRowClickGridColMapper.update(entity);
		return R.ok();
	}

	@Override
	public R settingSelectorModal(Long id, Long idYsModalSelector) {
		
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		if (idYsModalSelector == null) {
			return R.error("弹出框不能为空");
		}

		YsRowClickGridCol entity = new YsRowClickGridCol().setId(id).queryFirst();
		if (entity == null) {
			return R.error("数据不存在");
		}
		
		YsModalSelector ysModalSelector = new YsModalSelector().setId(idYsModalSelector).queryFirst();
		entity.setModalKey(ysModalSelector.getNameTable());
		ysRowClickGridColMapper.update(entity);

		return R.ok();
	}

	@Override
	public String toSettingMultyFileSourceType(Model model, Long id) {
		
		if (id == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsRowClickGridCol entity = new YsRowClickGridCol().setId(id).queryFirst();
		if (!YsRowClickGridColEnum.FrontType.FILE_MULTY.value.equals(entity.getFrontType())) {
			return W.error(model, "非多文件类型无法设置");
		}
		
		// 字典数组
		List<NameValueSelectDto> rows = new ArrayList<NameValueSelectDto>();
		{
			String name = "请选择";
			String value = "";
			boolean isSelected = entity.getMultyFileSourceType() == null ? true : false;
			rows.add(new NameValueSelectDto(name, value, isSelected));
		}
		for(TbFileInfoEnum.SourceType e : TbFileInfoEnum.SourceType.values()) {
			String name = e.name;
			String value = e.value.toString();
			boolean isSelected = entity.getMultyFileSourceType() == null ? false : e.value == entity.getMultyFileSourceType();
			rows.add(new NameValueSelectDto(name, value, isSelected));
		}

		String name = entity.getName();
		String code = entity.getCode();
		ToSettingMultyFileSourceTypeDto dto = new ToSettingMultyFileSourceTypeDto(id, name, code, rows);
		model.addAttribute("dto", dto);
		return "sys/ysRowClickGridCol/toSettingMultyFileSourceType";
	}

	@Override
	public R settingMultyFileSourceType(Long id, Integer multyFileSourceType) {
		
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		if (multyFileSourceType == null) {
			return R.error("来源类型不能为空");
		} else if (TbFileInfoEnum.SourceType.checkIn(multyFileSourceType) == false) {
			return R.error("来源类型不在指定范围内");
		}

		YsRowClickGridCol entity = new YsRowClickGridCol().setId(id).queryFirst();
		if (entity == null) {
			return R.error("数据不存在");
		}
		
		entity.setMultyFileSourceType(multyFileSourceType);
		ysRowClickGridColMapper.update(entity);

		return R.ok();
	}

	@Override
	public String toAddDictForSelector(Model model, Long colId) {
		
		if (colId == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsCol entity = new YsCol().setId(colId).queryFirst();
		
		String dictKey = entity.getDictKeyBase();
		if (dictKey == null || dictKey.length() == 0) {
			return W.error(model, "请先设置字典编码");
		}
		
		ToAddDictOfSelectorDto dto = new ToAddDictOfSelectorDto(colId, dictKey);

		model.addAttribute("dto", dto);
		return "sys/ysCol/toAddDictForSelector";
	}

	@Override
	public String toEditDictForSelector(Model model, Long dictId) {
		
		if (dictId == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsDict tbBaseInfo = new YsDict().setId(dictId).queryFirst();
		
		String dictKey = tbBaseInfo.getDictKey();
		String dictName = tbBaseInfo.getDictName();
		String dictValue = tbBaseInfo.getDictValue();
		ToEditDictForSelectorDto dto = new ToEditDictForSelectorDto(dictId, dictKey, dictName, dictValue);

		model.addAttribute("dto", dto);
		return "sys/ysCol/toEditDictForSelector";
	}

	@Override
	public R addDictForSelector(Long colId, String dictName, String dictValue) {
		
		if (colId == null) {
			return R.error("ID不能为空");
		}
		
		YsCol entity = new YsCol().setId(colId).queryFirst();
		
		String dictKey = entity.getDictKeyBase();
		if (dictKey == null || dictKey.length() == 0) {
			return R.error("字典编码不能为空");
		}
		
		if (dictValue == null || dictValue.length() == 0) {
			return R.error("字典值不能为空");
		}
		
		if (dictName == null || dictName.length() == 0) {
			return R.error("字典名称不能为空");
		}
		
		// 验证字典唯一性
		if (new TbBaseInfo().setVType(dictKey).setVId(dictValue).count() > 0) {
			return R.error("该字典已存在，不能重复添加");
		}
		YsDict ysDict = null;
		{
			Integer sortNo = diySqlMapper.getNextSortNoOfYsDict(dictKey);
			ysDict = YsDict.ysDict(dictKey, dictName, dictValue, sortNo, 1);
			ysDictMapper.insert(ysDict);
		}
		
		return R.ok("操作成功");
	}

	@Override
	public R editDictForSelector(Long dictId, String dictName, String dictValue) {
		
		if (dictId == null) {
			return R.error("字典ID不能为空");
		}
		
		YsDict ysDict = new YsDict().setId(dictId).queryFirst();
		
		if (dictValue == null || dictValue.length() == 0) {
			return R.error("字典值不能为空");
		}
		
		if (dictName == null || dictName.length() == 0) {
			return R.error("字典名称不能为空");
		}
		
		// 验证字典唯一性
		if (new YsDict().setDictKey(ysDict.getDictKey()).setDictValue(dictValue).where("A.Id != '" + dictId + "'").count() > 0) {
			return R.error("该字典已存在，不能重复添加");
		}
		
		ysDict.setDictValue(dictValue).setDictName(dictName);
		ysDictMapper.update(ysDict);
		
		return R.ok("操作成功");
	}
}
	

